export default {
	created() {
		if (Array.isArray(this.columns) && this.columns.length !== 0 && this.effective(this.columns)) {
			const path = this.$route.fullPath
			const cache = localStorage.getItem(path)
			if (cache) {
				const arr = cache.split(",")
				this.columns.forEach(item => {
					this.$set(item, "visible", !arr.includes(item.key.toString()))
				})
			}
		}
	},
	watch: {
		columns: {
			handler: function (val, oldVal) {
				if (Array.isArray(this.columns) && this.columns.length !== 0 && this.effective(val)) {
					const hiddenColumns = val.filter(item => !item.visible)
					const path = this.$route.fullPath
					if (hiddenColumns.length > 0) {
						const hiddenColumnsKey = hiddenColumns.map(item => item.key)
						localStorage.setItem(path, hiddenColumnsKey.join())
					} else {
						localStorage.removeItem(path)
					}
				}
			},
			deep: true
		}
	},
	methods: {
		effective(arr) {
			if (arr.length === 0) return
			const allValid = arr.every(obj => {
				const objKeys = Object.keys(obj)
				return objKeys.length === 3
			})
			return allValid
		}
	}
}
